<?php
declare(strict_types=1);

namespace Zlf\Mysql\Traits;

use PDO;
use Zlf\Mysql\Column;

trait Mysql
{

    /**
     * @describe 获取所有数据表信息
     * @user zlf zms-pro@qq.com 2025/1/18
     * @return array<array{table_name:string,engine:string,table_comment:string,table_collation:string,auto_increment:string,update_time:string}>
     *     -table_name 表的名称
     *     -engine 引擎
     *     -table_comment 表的注释或描述
     *     -table_collation 表默认字符集排序规则
     *     -auto_increment 下一个AUTO_INCREMENT值
     *     -update_time 表的名称
     */
    public function getTables(): array
    {
        $this->connect()->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
        $finds = ['table_name', 'engine', 'table_comment', 'table_collation', 'auto_increment', 'update_time'];
        $tables = $this->queryAll('SELECT ' . implode(',', $finds) . ' FROM INFORMATION_SCHEMA.TABLES WHERE `TABLE_SCHEMA`=:dbname', [':dbname' => $this->getDbname()]);
        $this->connect()->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
        return $tables;
    }


    /**
     * @describe 获取所有数据表信息
     * @user zlf zms-pro@qq.com 2025/1/18
     * @return array<array{table_name:string,engine:string,table_comment:string,table_collation:string,auto_increment:string,update_time:string}>
     *     -table_name 表的名称
     *     -engine 引擎
     *     -table_comment 表的注释或描述
     *     -table_collation 表默认字符集排序规则
     *     -auto_increment 下一个AUTO_INCREMENT值
     *     -update_time 表的名称
     */
    public function getTable(string $table): array
    {
        $this->connect()->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
        $info = $this->queryAll("SHOW FULL COLUMNS FROM ".$this->getDbname().".{$table}");
        $this->connect()->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
        if(!$info){
            throw new \Exception("Data table `{$table}` does not exist");
        }
        Column::info($info);
        return [];
    }

    /**
     * @describe 获取数据表数量
     * @return int
     * @user zlf zms-pro@qq.com 2025/1/18
     */
    public function getTablesCount(): int
    {
        $this->connect()->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
        $tables = $this->queryOne('SELECT COUNT(`table_name`) AS `count` FROM INFORMATION_SCHEMA.TABLES WHERE `TABLE_SCHEMA`=:dbname', [':dbname' => $this->getDbname()]);
        $this->connect()->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
        return $tables ? $tables['count'] : 0;
    }

    /**
     * @describe 获取数据表信息
     * @return string
     * @user zlf zms-pro@qq.com 2025/1/17
     */
    public function Version(): string
    {
        $res = $this->queryOne("SELECT VERSION() as version");
        if (is_array($res) && isset($res['version'])) {
            return $res['version'];
        }
        return '';
    }

    /**
     * @describe 获取数据库的字符集编码
     * @user zlf zms-pro@qq.com 2025/1/18
     * @return null|array{character:string,collation:string}
     */
    public function getDatabaseCharset(): ?array
    {
        $data = $this->queryOne("SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = DATABASE()");
        return $data ? [
            'character' => $data['DEFAULT_CHARACTER_SET_NAME'],
            'collation' => $data['DEFAULT_COLLATION_NAME'],
        ] : null;
    }
}